package cn.fz.demo03.tools;

import cn.fz.demo03.entity.po.Course;
import cn.fz.demo03.entity.po.CourseReservation;
import cn.fz.demo03.entity.po.School;
import cn.fz.demo03.entity.query.CourseQuery;
import cn.fz.demo03.service.ICourseReservationService;
import cn.fz.demo03.service.ICourseService;
import cn.fz.demo03.service.ISchoolService;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.List;

@RequiredArgsConstructor
@Component
public class CourseTools {
    private final ICourseService courseService;
    private final ISchoolService schoolService;
    private final ICourseReservationService courseReservationService;

    @Tool(description = "根据条件查询课程")
    public List<Course> queryCourse(@ToolParam(description = "查询的条件", required = false) CourseQuery query){
        if (null == query){
            // return List.of();
            return courseService.list(); // 没有条件的时候都返回
        }
        QueryChainWrapper<Course> wrapper = courseService.query()
                .eq(null != query.getType(), "type", query.getType()) // type = '编程'
                .le(null != query.getEdu(), "edu", query.getEdu()); // edu <=2
        if (null != query.getSorts() && !query.getSorts().isEmpty()){
            for (CourseQuery.Sort sort : query.getSorts()) {
                wrapper.orderBy(true, sort.getAsc(), sort.getField());
            }
        }
        return wrapper.list();
    }

    @Tool(description = "查询所有校区")
    public List<School> querySchool(){
        return schoolService.list();
    }

    @Tool(description = "生成预约单，返回预约单号")
    public Integer createCourseReservation(
            @ToolParam(description = "预约课程") String course,
            @ToolParam(description = "预约校区") String school,
            @ToolParam(description = "学生姓名") String studentName,
            @ToolParam(description = "联系电话") String contactInfo,
            @ToolParam(description = "备注",required = false) String remark){
        CourseReservation reservation = new CourseReservation()
                .setCourse(course)
                .setSchool(school)
                .setStudentName(studentName)
                .setContactInfo(contactInfo)
                .setRemark(remark);
        return courseReservationService.save(reservation) ? reservation.getId() : 0;
    }
}
